local skel = fk.CreateSkill{--定义
  name = "kt_st__fubaiload",
}

skel:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    local room = player.room
    return target == player and player:hasSkill(skel.name) and
    data.card.name == player:getMark("@kt_st__fubai")
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(player:getMark("kt_st__fubai2"))
    if not to or to.dead then return end
    room:handleAddLoseSkills(player,"-"..skel.name)
    room:handleAddLoseSkills(to,"kt_st__fubai")
    room:setPlayerMark(player,"@kt_st__fubai",0)
    room:setPlayerMark(player,"kt_st__fubai2",0)
    room:doIndicate(player, {to})
  end,
})

Fk:addPoxiMethod{
  name = "kt_st__fubaiload_ask",
  prompt = "#kt_st__fubai2_ask",
  card_filter = function(to_select, selected, data)
    if #selected < 2 then
      if #selected == 0 then
        return true
      else
        if table.contains(data[1][2], selected[1]) then
          return table.contains(data[2][2], to_select)
        else
          return table.contains(data[1][2], to_select)
        end
      end
    end
  end,
  feasible = function(selected)
    return #selected == 2
  end,
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    local list = table.filter(player.room:getOtherPlayers(player),function (p)
      return p.kingdom == player.kingdom and not p:isNude()
    end)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
    and not player:isNude() and #list > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local list = table.filter(room:getOtherPlayers(player),function (p)
      return p.kingdom == player.kingdom and not p:isNude()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 999,
      targets = list,
      skill_name = skel.name,
      prompt = "#kt_st__fubai1_ask",
      cancelable = true,
      --no_indicate = true
    })
    if #to > 0 then
      event:setCostData(self, {extra_data = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, "kt_st__fubai")
    player:broadcastSkillInvoke("kt_st__fubai")
    local room = player.room
    local players = event:getCostData(self).extra_data
    room:doIndicate(player, {players})
    for _,p in ipairs(players) do
      if player:isNude() or p:isNude() then return end
      --
      local result = room:askToPoxi(player, {
        poxi_type = "kt_st__fubaiload_ask",
        data = {
          { player.general, player:getCardIds("he") },
          { p.general, p:getCardIds("he") },
        },
        cancelable = false,
      })
      if #result ~= 2 then return end
      local id1, id2 = result[1], result[2]
      if table.contains(p:getCardIds("he"), result[2]) then
        id1, id2 = result[2], result[1]
      end
      local moves = {}
      table.insert(moves, {
        ids = {id1},
        from = p,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = skel.name,
        proposer = player,
        moveVisible = false,
        --drawPilePosition = table.indexOf(cards, id2),
      })
      table.insert(moves, {
        ids = {id2},
        from = player,
        to = p,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = skel.name,
        proposer = player,
        moveVisible = false,
      })
      room:moveCards(table.unpack(moves))
      --
    end
  end,
})

return skel